using CommunityToolkit.Maui.Core.Primitives;
using CommunityToolkit.Maui.Views;
using MauiApp1.ViewModels;
using Microsoft.Maui.Controls;

namespace MauiApp1.Views;

public partial class VideoInfoPage : ContentPage
{
    private VideoInfoPageViewModel model;

    public VideoInfoPage()
	{
		InitializeComponent();
        model = new VideoInfoPageViewModel();
        model.media = mediaElement;
        this.BindingContext = model;

	}

    //private void OnPlayPauseButtonClicked(object sender, EventArgs e)
    //{
    //    if (mediaElement.CurrentState == MediaElementState.Stopped ||
    //    mediaElement.CurrentState == MediaElementState.Paused)
    //    {
    //        mediaElement.Play();
    //    }
    //    else if (mediaElement.CurrentState == MediaElementState.Playing)
    //    {
    //        mediaElement.Pause();
    //    }
    //}

    //private void OnStopButtonClicked(object sender, EventArgs e)
    //{
    //    mediaElement.Stop();
    //}

    private void ContentPage_Unloaded(object sender, EventArgs e)
    {
        mediaElement.Handler?.DisconnectHandler();
        model.orientationService.SetDeviceOrientation(DisplayOrientation.Portrait);
    }

    private void Slider_DragStarted(object sender, EventArgs e)
    {
        mediaElement.Pause();
    }

    private async void Slider_DragCompleted(object sender, EventArgs e)
    {
        var newValue = ((Slider)sender).Value;
        await mediaElement.SeekTo(TimeSpan.FromSeconds(newValue), CancellationToken.None);

        mediaElement.Play();
    }

    private void VSlider_DragStarted(object sender, EventArgs e)
    {

    }

    private void VSlider_DragCompleted(object sender, EventArgs e)
    {
        var newValue = ((Slider)sender).Value;
        mediaElement.Volume = newValue;
    }

    private void ContentPage_Loaded(object sender, EventArgs e)
    {
#if WINDOWS

                   model.FullHeight = this.Height;
#endif

    }

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
#if WINDOWS

                   model.FullHeight = this.Height;
#endif
    }

    private void mediaElement_MediaEnded(object sender, EventArgs e)
    {
        model.NexiUrl(new object());
    }
}